package com.jic.product.impl;

import java.lang.Long;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.product.entity.ProductBrand;
import com.jic.product.mapper.ProductBrandMapper;
import com.jic.product.manage.request.ProductBrandEditRequest;
import com.jic.product.manage.request.ProductBrandRequest;
import com.jic.product.manage.response.ProductBrandResponse;
import com.jic.product.service.ProductBrandService;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 商品品牌表
 * @author : 
 * @email : 
 * @since : 2020-01-10 07:03:01
 * @version : v1.0.0
 */
@Service
public class ProductBrandServiceImpl implements ProductBrandService{

    @Autowired
    private ProductBrandMapper productBrandMapper;

    @Override
    public long insert(ProductBrandEditRequest productBrandEditRequest) {
        long flag = productBrandMapper.insert(productBrandEditRequest);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return productBrandMapper.delete(id);
    }

    @Override
    public long update(ProductBrandEditRequest productBrandEditRequest) {
        return productBrandMapper.update(productBrandEditRequest);
    }

    @Override
    public ProductBrand queryByPrimaryKey(Long id){
        return productBrandMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<ProductBrandResponse> query(ProductBrandRequest productBrandRequest){
        return productBrandMapper.query(productBrandRequest);
    }
    @Override
    public PageResult<ProductBrandResponse> queryList(ProductBrandRequest productBrandRequest){
        PageHelper.startPage(productBrandRequest.getStart(), productBrandRequest.getLimit());
        List<ProductBrandResponse> list = productBrandMapper.query(productBrandRequest);
        PageInfo<ProductBrandResponse> pageInfo = new PageInfo<>(list);
        ProductBrand productBrand = new ProductBrand();
        BeanUtils.copyProperties(productBrandRequest,productBrand);
//        long count = productBrandMapper.queryPageCount(productBrand);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(pageInfo.getTotal());
        return pageResult;
    }

    @Override
    public PageResult<ProductBrand> queryPage(ProductBrand productBrand, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductBrand> list = productBrandMapper.queryPage(startIndex, pageQuery.getLimit(), productBrand);
        long count = productBrandMapper.queryPageCount(productBrand);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public PageResult<ProductBrand> listRestPage(ProductBrand productBrand, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<ProductBrand> list = productBrandMapper.listRestPage(startIndex, pageQuery.getLimit(), productBrand);
        long count = productBrandMapper.listRestPageCount(productBrand);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    /**
     * 获取商品品牌中的所有品牌来源
     * @return
     */
    @Override
    public RestResult querySourceList() {
        List<String> querySource = productBrandMapper.querySourceList();
        return RestResult.success(querySource);
    }
}